package com.easyidea.its.util;

import java.util.Properties;

import com.infowarelab.common.config.ConfigUtil;
import com.infowarelab.common.log.Log;
import com.infowarelab.common.log.LogFactory;

public final class Config {

	private static Log log = LogFactory.getLog(Config.class);

	private Config() {
	}

	private static Properties props = null;

	private static String configFile = System.getProperty("WEB_CONFIG_HOME") + "/its.properties";

	/**
	 * TODO 根据service version id获取service name 根据版本情况，加载相应的配置文件
	 */

	private static boolean changeToBoolean(String str) {
		String tmp = str.toLowerCase();
		if (tmp.equals("true")) {
			return true;
		} else if (tmp.equals("false")) {
			return false;
		} else {
			throw new RuntimeException("class not matching.");
		}
	}

	/**
	 * convert string to boolean
	 * 
	 * @param key
	 * @return
	 */
	public static boolean getBoolean(String key) {
		String str = Config.getString(key);
		try {
			return Config.changeToBoolean(str);
		} catch (Exception e) {
			return false;
		}
	}

	/**
	 * convert string to boolean
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static boolean getBoolean(String key, boolean defaultValue) {
		String str = Config.getString(key);
		try {
			return Config.changeToBoolean(str);
		} catch (Exception e) {
			return defaultValue;
		}
	}

	private static int changeToInt(String str) throws Exception {
		return Integer.parseInt(str);
	}

	/**
	 * convert string to int
	 * 
	 * @param key
	 * @return
	 */
	public static int getInt(String key) {
		String str = Config.getString(key);
		try {
			return Config.changeToInt(str);
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * convert string to int
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static int getInt(String key, int defaultValue) {
		String str = Config.getString(key);
		try {
			return Config.changeToInt(str);
		} catch (Exception e) {
			return defaultValue;
		}
	}

	/**
	 * get value by key
	 * 
	 * @param key
	 * @param defaultValue
	 * @return
	 */
	public static String getString(String key, String defaultValue) {

		String tmp = getString(key);
		if (tmp == null) {
			tmp = defaultValue;
		}
		log.debug(key + ": " + tmp);
		return tmp;
	}

	private static String getRealValue(String valueStr) {
		if (valueStr == null)
			return null;
		String rtnValue = valueStr;
		while (valueStr.indexOf("${") >= 0 && valueStr.indexOf("}") >= 0) {
			String keyName = valueStr.substring(valueStr.indexOf("{") + 1, valueStr.indexOf("}"));
			rtnValue = getString(keyName, "") + valueStr.substring(valueStr.indexOf("}") + 1);
		}
		return rtnValue;
	}

	/**
	 * get value by key
	 * 
	 * @param key
	 * @return
	 */
	public static String getString(String key) {
		initProps();

		String tmp = getRealValue(toChinese(props.getProperty(key)));
		log.debug(key + ": " + tmp);
		return tmp;
	}
	private static  String   toChinese(String   strvalue){     
	    
        try{     
            if(strvalue==null)     
                return   "";     
            else{     
                strvalue   =   new   String(strvalue.getBytes("ISO8859_1"),   "GBK");     
                return   strvalue;     
            }     
        }catch(Exception   e){     
            return   "";     
        }     
  }  
	private static void initProps() {
		if (null == props) {
			props = new Properties();
			try {
				props.putAll(ConfigUtil.getProperties("its"));
			} catch (Exception ex) {
				log.error(ex);
			}
//			try {
//				props.putAll(ResourcesUtil.getResourceAsProperties("project.properties"));
//				props.load(new FileInputStream(configFile));
//			} catch (Exception ex) {
//				log.error(ex);
//			}
		}
	}


	/**
	 * Add new property file
	 * 
	 * @param propertiesFile
	 */
	public static void setPropertyFile(String propertiesFile) {
		initProps();
		props.putAll(ConfigUtil.getProperties(propertiesFile));
	}
}
